#include "..\\code\\dataStruct.h"
#include "..\\code\\common.h"
#include <math.h>
#include <stdio.h>

void zeroCurrh(tranDevice* trandevice){
    Phasor Ua, Ub, Uc;
    Phasor Ia, Ib, Ic;
    Phasor U0, I0;
    double angle;
    double Iset1, Iset2, Iset3;
    double time1, time2, time3;
    double time;
    double startTime;
    time = trandevice->time;
    startTime = trandevice->backTime[0];

    Iset1 = trandevice->zeroCur1[0] * (trandevice->ct1);
    Iset2 = trandevice->zeroCur1[1] * (trandevice->ct1);
    Iset3 = trandevice->zeroCur1[2] * (trandevice->ct1);

    time1 = trandevice->zeroTime1[0];
    time2 = trandevice->zeroTime1[1];
    time3 = trandevice->zeroTime1[2];

    Ua = trandevice->phasor[0];
    Ub = trandevice->phasor[1];
    Uc = trandevice->phasor[2];

    Ia = trandevice->phasor[3];
    Ib = trandevice->phasor[4];
    Ic = trandevice->phasor[5];

    Ia = phasorNumMulti(trandevice->ct1, Ia);
    Ib = phasorNumMulti(trandevice->ct1, Ib);
    Ic = phasorNumMulti(trandevice->ct1, Ic);

    U0 = phasorSeq(Ua, Ub, Uc, 0);
    I0 = phasorAdd(Ia, Ib);
    I0 = phasorAdd(I0, Ic);
//    I0 = phasorSeq(Ia, Ib, Ic, 0);
    angle = phasorAngleDiff(U0, I0);
    int direct;

    if((angle> 165) && (angle <345)){
        direct = 0; //指向系统
    }
    else{
        direct = 1; //指向变压器
    }


    int directFlag1, directFlag2;
    if(((direct == 0) && ((trandevice->backEnable1[5] < 1)) || (trandevice->backEnable1[4] < 1))){
        directFlag1 = 1;
    }
    if(((direct == 1) && ((trandevice->backEnable1[5] > 0)) || (trandevice->backEnable1[4] < 1))){
        directFlag1 = 1;
    }
    if(((direct == 0) && ((trandevice->backEnable1[5] < 1)) || (trandevice->backEnable1[7] < 1))){
        directFlag2 = 1;
    }
    if(((direct == 1) && ((trandevice->backEnable1[5] > 0)) || (trandevice->backEnable1[7] < 1))){
        directFlag2 = 1;
    }


    //I
    if((phasorAbs(I0) > Iset1) && ((time - startTime) > (0.02 + time1)) && (directFlag1 == 1)){

        trandevice->tranProRes[1] = 1;
        trandevice->tranProRes[2] = 1;
        trandevice->tranProRes[0] = 1;
        trandevice->mainFlag = 1;

        tranWriteLog(trandevice, "高零序过流I段动作");
    }
    if((phasorAbs(I0) > Iset2) && ((time - startTime) > (0.02 + time2)) && (directFlag2 == 1)){

        trandevice->tranProRes[1] = 1;
        trandevice->tranProRes[2] = 1;
        trandevice->tranProRes[0] = 1;
        trandevice->mainFlag = 1;

        tranWriteLog(trandevice, "高零序过流II段动作");
    }
    if((phasorAbs(I0) > Iset3) && ((time - startTime) > (0.02 + time3))){

        trandevice->tranProRes[1] = 1;
        trandevice->tranProRes[2] = 1;
        trandevice->tranProRes[0] = 1;
        trandevice->mainFlag = 1;

        tranWriteLog(trandevice, "高零序过流III段动作");
    }

}

void zeroCurrv(tranDevice* trandevice){
    Phasor Ua, Ub, Uc;
    Phasor Ia, Ib, Ic;
    Phasor U0, I0;
    double angle;
    double Iset1, Iset2, Iset3;
    double time1, time2, time3;
    double startTime;
    double time;
    time = trandevice->time;
    startTime = trandevice->backTime[1];


    Iset1 = trandevice->zeroCur2[0] * (trandevice->ct2);
    Iset2 = trandevice->zeroCur2[1] * (trandevice->ct2);
    Iset3 = trandevice->zeroCur2[2] * (trandevice->ct2);

    time1 = trandevice->zeroTime2[0];
    time2 = trandevice->zeroTime2[1];
    time3 = trandevice->zeroTime2[2];

    Ua = trandevice->phasor[6];
    Ub = trandevice->phasor[7];
    Uc = trandevice->phasor[8];

    Ia = trandevice->phasor[9];
    Ib = trandevice->phasor[10];
    Ic = trandevice->phasor[11];

    Ia = phasorNumMulti(trandevice->ct2, Ia);
    Ib = phasorNumMulti(trandevice->ct2, Ib);
    Ic = phasorNumMulti(trandevice->ct2, Ic);

    U0 = phasorSeq(Ua, Ub, Uc, 0);
    I0 = phasorAdd(Ia, Ib);
    I0 = phasorAdd(I0, Ic);
//    I0 = phasorSeq(Ia, Ib, Ic, 0);
    angle = phasorAngleDiff(U0, I0);
    int direct;

    if((angle> 165) && (angle <345)){
        direct = 0; //指向系统
    }
    else{
        direct = 1; //指向变压器
    }


    int directFlag1, directFlag2;
    if(((direct == 0) && ((trandevice->backEnable2[5] < 1)) || (trandevice->backEnable2[4] < 1))){
        directFlag1 = 1;
    }
    if(((direct == 1) && ((trandevice->backEnable2[5] > 0)) || (trandevice->backEnable2[4] < 1))){
        directFlag1 = 1;
    }
    if(((direct == 0) && ((trandevice->backEnable2[5] < 1)) || (trandevice->backEnable2[7] < 1))){
        directFlag2 = 1;
    }
    if(((direct == 1) && ((trandevice->backEnable2[5] > 0)) || (trandevice->backEnable2[7] < 1))){
        directFlag2 = 1;
    }


    //I
    if((phasorAbs(I0) > Iset1) && ((time - startTime) > (0.02 + time1)) && (directFlag1 == 1)){

        trandevice->tranProRes[1] = 1;
        trandevice->tranProRes[2] = 1;
        trandevice->tranProRes[0] = 1;
        trandevice->mainFlag = 1;

        if(trandevice->ratedV30 == 0){
            tranWriteLog(trandevice, "低零序过流I段动作");
        }
        else{
            tranWriteLog(trandevice, "中零序过流I段动作");
        }

    }
    if((phasorAbs(I0) > Iset2) && ((time - startTime) > (0.02 + time2)) && (directFlag2 == 1)){

        trandevice->tranProRes[1] = 1;
        trandevice->tranProRes[2] = 1;
        trandevice->tranProRes[0] = 1;
        trandevice->mainFlag = 1;

        if(trandevice->ratedV30 == 0){
            tranWriteLog(trandevice, "低零序过流II段动作");
        }
        else{
            tranWriteLog(trandevice, "中零序过流II段动作");
        }
    }
    if((phasorAbs(I0) > Iset3) && ((time - startTime) > (0.02 + time3))){

        trandevice->tranProRes[1] = 1;
        trandevice->tranProRes[2] = 1;
        trandevice->tranProRes[0] = 1;
        trandevice->mainFlag = 1;

        if(trandevice->ratedV30 == 0){
            tranWriteLog(trandevice, "低零序过流III段动作");
        }
        else{
            tranWriteLog(trandevice, "中零序过流III段动作");
        }

    }

}

void zeroCurrt(tranDevice* trandevice){
    Phasor Ia, Ib, Ic;
    Phasor I0;
    double angle;
    double Iset1;
    double time1;
    double startTime;
    double time;
    time = trandevice->time;
    startTime = trandevice->backTime[2];


    Iset1 = trandevice->zeroCur2[3]  * (trandevice->ct5);
    time1 = trandevice->zeroTime2[3];

    Ia = trandevice->phasor[21];
    Ib = trandevice->phasor[22];
    Ic = trandevice->phasor[23];
    Ia = phasorNumMulti(trandevice->ct5, Ia);
    Ib = phasorNumMulti(trandevice->ct5, Ib);
    Ic = phasorNumMulti(trandevice->ct5, Ic);

    I0 = phasorAdd(Ia, Ib);
    I0 = phasorAdd(I0, Ic);
//    I0 = phasorSeq(Ia, Ib, Ic, 0);

    if((phasorAbs(I0) > Iset1) && ((time - startTime) > (0.02 + time1))){

        trandevice->tranProRes[1] = 1;
        trandevice->tranProRes[2] = 1;
        trandevice->tranProRes[0] = 1;
        trandevice->mainFlag = 1;

        tranWriteLog(trandevice, "公共绕组零序过流动作");
    }


}

void compCurrh(tranDevice* trandevice){
    Phasor Ua, Ub, Uc, Uca, Uab, Ubc;
    Phasor Ia, Ib, Ic;
    Phasor U1, I1;
    Phasor Z1;
    double angle;
    double Imax;
    double Umax;
    Phasor U2;
    int direct;

    double Iset, Uset1, Uset2;
    double time, time1;
    double startTime;
    time = trandevice->time;
    startTime = trandevice->backTime[3];

    Uset1 = trandevice->compSet1[0] * (trandevice->pt1);
    Uset2 = trandevice->compSet1[1] * (trandevice->pt1);

    Iset = trandevice->compSet1[2] * (trandevice->ct1);
    time1 = trandevice->compSet1[3];

    Ua = trandevice->phasor[0];
    Ub = trandevice->phasor[1];
    Uc = trandevice->phasor[2];
    Ua = phasorNumMulti(trandevice->pt1, Ua);
    Ub = phasorNumMulti(trandevice->pt1, Ub);
    Uc = phasorNumMulti(trandevice->pt1, Uc);

    Uca = phasorSub(Ua, Uc);
    Uab = phasorSub(Ub, Ua);
    Ubc = phasorSub(Uc, Ub);

    Ia = trandevice->phasor[3];
    Ib = trandevice->phasor[4];
    Ic = trandevice->phasor[5];
    Ia = phasorNumMulti(trandevice->ct1, Ia);
    Ib = phasorNumMulti(trandevice->ct1, Ib);
    Ic = phasorNumMulti(trandevice->ct1, Ic);

    U1 = phasorSeq(Ua, Ub, Uc, 1);
    U2 = phasorSeq(Ua, Ub, Uc, 2);
    I1 = phasorSeq(Ia, Ib, Ic, 1);
    Z1 = phasorDiv(U1, I1);
    angle = phasorAngle(Z1);

    Umax = phasorAbs(Uca);
    if(Umax < phasorAbs(Uab)){
        Umax = phasorAbs(Uab);
    }
    if(Umax < phasorAbs(Ubc)){
        Umax = phasorAbs(Ubc);
    }
    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }


    if((angle> 45)  && (angle <225)){
        direct = 1;
    }
    else{
        direct = 0;
    }

    if((phasorAbs(U2) > Uset2) && (direct == 1) && (Umax < Uset1) && ((time -startTime) > (0.02 + time1))){
        int i = 0;
        for(i=0; i<3 ; i++){
            trandevice->tranProRes[i] = 1;
            trandevice->mainFlag = 1;
        }
        tranWriteLog(trandevice, "高压侧复压过流动作");
    }

}


void compCurrv(tranDevice* trandevice){
    Phasor Ua, Ub, Uc, Uca, Uab, Ubc;
    Phasor Ia, Ib, Ic;
    Phasor U1, I1;
    Phasor Z1;
    double angle;
    double Imax;
    double Umax;
    Phasor U2;
    int direct;

    double Iset, Uset1, Uset2;
    double time, time1;
    double startTime;
    time = trandevice->time;
    startTime = trandevice->backTime[4];

    Uset1 = trandevice->compSet2[0] * (trandevice->pt2);
    Uset2 = trandevice->compSet2[1] * (trandevice->pt2);
    Iset = trandevice->compSet2[2] * (trandevice->ct2);
    time1 = trandevice->compSet2[3];

    Ua = trandevice->phasor[6];
    Ub = trandevice->phasor[7];
    Uc = trandevice->phasor[8];
    Ua = phasorNumMulti(trandevice->pt2, Ua);
    Ub = phasorNumMulti(trandevice->pt2, Ub);
    Uc = phasorNumMulti(trandevice->pt2, Uc);

    Uca = phasorSub(Ua, Uc);
    Uab = phasorSub(Ub, Ua);
    Ubc = phasorSub(Uc, Ub);

    Ia = trandevice->phasor[9];
    Ib = trandevice->phasor[10];
    Ic = trandevice->phasor[11];
    Ia = phasorNumMulti(trandevice->ct2, Ia);
    Ib = phasorNumMulti(trandevice->ct2, Ib);
    Ic = phasorNumMulti(trandevice->ct2, Ic);

    U1 = phasorSeq(Ua, Ub, Uc, 1);
    U2 = phasorSeq(Ua, Ub, Uc, 2);
    I1 = phasorSeq(Ia, Ib, Ic, 1);
    Z1 = phasorDiv(U1, I1);
    angle = phasorAngle(Z1);

    Umax = phasorAbs(Uca);
    if(Umax < phasorAbs(Uab)){
        Umax = phasorAbs(Uab);
    }
    if(Umax < phasorAbs(Ubc)){
        Umax = phasorAbs(Ubc);
    }
    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if(angle> 45  && angle <225){
        direct = 1;
    }
    else{
        direct = 0;
    }

    if((phasorAbs(U2) > Uset2) && (direct == 1) && (Umax < Uset1) && ((time -startTime) > (0.02 + time1))){
        int i = 0;
        for(i=0; i<3 ; i++){
            trandevice->tranProRes[i] = 1;
            trandevice->mainFlag = 1;
        }
        if(trandevice->ratedV30 == 0){
            tranWriteLog(trandevice, "低压侧复压过流动作");
        }
        else{
            tranWriteLog(trandevice, "中压侧复压过流动作");
        }
    }

}


void compCurrl1(tranDevice* trandevice){
    Phasor Ua, Ub, Uc, Uca, Uab, Ubc;
    Phasor Ia, Ib, Ic;
    Phasor U1, I1;
    Phasor Z1;
    double angle;
    double Imax;
    double Umax;
    Phasor U2;
    int direct;

    double Iset, Uset1, Uset2;
    double time, time1;
    double startTime;
    time = trandevice->time;
    startTime = trandevice->backTime[5];

    Uset1 = trandevice->compSet3[0] * (trandevice->pt3);
    Uset2 = trandevice->compSet3[1] * (trandevice->pt3);
    Iset = trandevice->compSet3[2] * (trandevice->ct3);
    time1 = trandevice->compSet3[3];

    Ua = trandevice->phasor[12];
    Ub = trandevice->phasor[13];
    Uc = trandevice->phasor[14];

    Ua = phasorNumMulti(trandevice->pt3, Ua);
    Ub = phasorNumMulti(trandevice->pt3, Ub);
    Uc = phasorNumMulti(trandevice->pt3, Uc);

    Uca = phasorSub(Ua, Uc);
    Uab = phasorSub(Ub, Ua);
    Ubc = phasorSub(Uc, Ub);

    Ia = trandevice->phasor[15];
    Ib = trandevice->phasor[16];
    Ic = trandevice->phasor[17];

    Ia = phasorNumMulti(trandevice->ct3, Ia);
    Ib = phasorNumMulti(trandevice->ct3, Ib);
    Ic = phasorNumMulti(trandevice->ct3, Ic);

    U1 = phasorSeq(Ua, Ub, Uc, 1);
    U2 = phasorSeq(Ua, Ub, Uc, 2);
    I1 = phasorSeq(Ia, Ib, Ic, 1);
    Z1 = phasorDiv(U1, I1);
    angle = phasorAngle(Z1);

    Umax = phasorAbs(Uca);
    if(Umax < phasorAbs(Uab)){
        Umax = phasorAbs(Uab);
    }
    if(Umax < phasorAbs(Ubc)){
        Umax = phasorAbs(Ubc);
    }
    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if(angle> 45  && angle <225){
        direct = 0;
    }
    else{
        direct = 1;
    }

    if((phasorAbs(U2) > Uset2) && (direct == 1) && (Umax < Uset1) && ((time -startTime) > (0.02 + time1))){
        int i = 0;
        for(i=0; i<3 ; i++){
            trandevice->tranProRes[i] = 1;
            trandevice->mainFlag = 1;
        }
        tranWriteLog(trandevice, "低压侧复压过流动作");
    }

}


void compCurrl2(tranDevice* trandevice){
    Phasor Ua, Ub, Uc, Uca, Uab, Ubc;
    Phasor Ia, Ib, Ic;
    Phasor U1, I1;
    Phasor Z1;
    double angle;
    double Imax;
    double Umax;
    Phasor U2;
    int direct;

    double Iset, Uset1, Uset2;
    double time, time1;
    double startTime;
    time = trandevice->time;
    startTime = trandevice->backTime[6];

    Uset1 = trandevice->compSet2[0] * (trandevice->pt3);
    Uset2 = trandevice->compSet2[1] * (trandevice->pt3);
    Iset = trandevice->compSet2[2] * (trandevice->ct4);
    time1 = trandevice->compSet2[3];

    Ua = trandevice->phasor[12];
    Ub = trandevice->phasor[13];
    Uc = trandevice->phasor[14];

    Ua = phasorNumMulti(trandevice->pt3, Ua);
    Ub = phasorNumMulti(trandevice->pt3, Ub);
    Uc = phasorNumMulti(trandevice->pt3, Uc);

    Uca = phasorSub(Ua, Uc);
    Uab = phasorSub(Ub, Ua);
    Ubc = phasorSub(Uc, Ub);

    Ia = trandevice->phasor[18];
    Ib = trandevice->phasor[19];
    Ic = trandevice->phasor[20];

    Ia = phasorNumMulti(trandevice->ct4, Ia);
    Ib = phasorNumMulti(trandevice->ct4, Ib);
    Ic = phasorNumMulti(trandevice->ct4, Ic);

    U1 = phasorSeq(Ua, Ub, Uc, 1);
    U2 = phasorSeq(Ua, Ub, Uc, 2);
    I1 = phasorSeq(Ia, Ib, Ic, 1);
    Z1 = phasorDiv(U1, I1);
    angle = phasorAngle(Z1);

    Umax = phasorAbs(Uca);
    if(Umax < phasorAbs(Uab)){
        Umax = phasorAbs(Uab);
    }
    if(Umax < phasorAbs(Ubc)){
        Umax = phasorAbs(Ubc);
    }
    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if(angle> 45  && angle <225){
        direct = 1; //指向系统；
    }
    else{
        direct = 0; //指向变压器
    }

    if((phasorAbs(U2) > Uset2) && (direct == 1) && (Umax < Uset1) && ((time -startTime) > (0.02 + time1))){
        int i = 0;
        for(i=0; i<3 ; i++){
            trandevice->tranProRes[i] = 1;

        }
        trandevice->mainFlag = 1;
        tranWriteLog(trandevice, "低绕组复压过流动作");
    }

}